/**
 * 通用js方法封装处理
 * Copyright (c) 2019 aidex
 */

export function replaceAll(text, stringToFind, stringToReplace) {
	if (stringToFind == stringToReplace) return this;
	var temp = text;
	var index = temp.indexOf(stringToFind);
	while (index != -1) {
		temp = temp.replace(stringToFind, stringToReplace);
		index = temp.indexOf(stringToFind);
	}
	return temp;
}
/**
 * @param {Object} obj {a:1,b:2,c:'' | null | undefined}
 * @return a=1&b=2
 */
export function stringify(obj) {
	if (typeof obj !== 'object' || Array.isArray(obj)) return
	const result = []
	for (const key in obj) {
		if (obj[key] !== null) {
			result.push(`${key}=${obj[key]}`)
		}
	}
	return result.join('&')
}
/**
 * @param {string} url
 * @param {string} name
 * 
 */
export function getQueryString(url, name) {
	let queryStr = url&& url.split('?')[1]
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
	var r = queryStr.match(reg); //匹配目标参数
	if (r != null) return decodeURI(r[2]);
	return null;
}

/**
 * @权限校验
 * @param {string} permissionFlag 
 * @returns Boolean
 */
export function hasPermi(permissionFlag){
  const allPermission = '*:*:*'
  const permissions = uni.getStorageSync('permissions') || []
  return permissions.some(permission => {
    return allPermission === permission || permissionFlag.includes(permission)
  })
}